package com.edais.service.imp;

import com.edais.actions.PerMatchDetailAction;
import com.edais.dao.InvitationDao;
import com.edais.dao.SystemSettingDao;
import com.edais.domain.Invitation;
import com.edais.service.InvitationService;
import com.edais.vo.InvitationVo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service

@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
public class InvitationServiceImp implements InvitationService {
	private static final Log logger = LogFactory.getLog(PerMatchDetailAction.class);

	@Autowired
	private InvitationDao invitationDao;

	@Autowired
	private SystemSettingDao systemSettingDao;

	/**
	 * 获取我的邀请码
	 * @param userId
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class,readOnly=false)
	public Map<String, Object> getMyInvitationCd(String userId) throws Exception{
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		
		// 取得我的邀请码
		String invitationCd = this.invitationDao.getMyInvitationCdById(userId);
		
		//TODO: 设置连接地址,暂定为固定值
//		String linkAddress = "http://www.onlinecredit.cn/EDAIWeb/index/indexs";
		
		rtnMap.put("invitationCd", invitationCd == null ? "" : invitationCd);
//		rtnMap.put("linkAddress", linkAddress);
		
		return rtnMap;
	}
	
	/**
	 * 获取活动文案
	 * @param userId
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class,readOnly=false)
	public Map<String, Object> getActivityRecord(String userId) throws Exception{
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		
		// 获取文案
		Map<String,String> record = invitationDao.getActivityRecord(userId);
		// 取得我的邀请码
		String invitationCd = this.invitationDao.getMyInvitationCdById(userId);
		String count ="0";
		// 用户有邀请码
		if (invitationCd != null && !"".equals(invitationCd)) {
			// 获取我的邀请人数
			count = this.invitationDao.getInvitationCountByCd(invitationCd);
		}
		rtnMap.put("bigActivityRecord", record.get("bigActivityRecord"));
		rtnMap.put("smallActivityRecord", record.get("smallActivityRecord"));
		rtnMap.put("count", count);
		return rtnMap;
	}
	
	/**
	 * 获取我的邀请列表
	 * @param userId
	 * @param current
	 * @param pageSize
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class,readOnly=false)
	public Map<String, Object> getMyInvitationList(String userId, Integer current, Integer pageSize) throws Exception{
		Map<String, Object> rtnMap = new HashMap<String, Object>() {};
		List<InvitationVo> list = new ArrayList<InvitationVo>();
		
		Integer rowBegin = (current - 1) * pageSize;
		Integer rowEnd = current * pageSize;
		// 取得我的邀请码
		String invitationCd = this.invitationDao.getMyInvitationCdById(userId);
		
		// 用户有邀请码
		if (invitationCd != null && !"".equals(invitationCd)) {
			// 获取我的邀请人数
			String count = this.invitationDao.getInvitationCountByCd(invitationCd);

			if (Integer.valueOf(count) > 0) {
				// 获取邀请列表
				List<Invitation> tmpList = this.invitationDao.getInvitationListByCd(invitationCd, rowBegin, rowEnd);
				for (Invitation invitation : tmpList) {
					InvitationVo invitationVo = new InvitationVo();
					// 用户id
					invitationVo.setUserId(invitation.getUserId());
					// 手机或邮箱
					invitationVo.setMobile(invitation.getMobile());
					// 好友姓名
					invitationVo.setUserName(invitation.getUserName());
					// 计划编号
					invitationVo.setOrderId(invitation.getOrderId());
					// 邀请时间
					invitationVo.setInvitedTime(this.changeDateFormat(invitation.getInvitedTime()));
					// 邀请结果
					invitationVo.setInvitedRes(invitation.getInvitedRes());
					// 理财产品名称
					invitationVo.setFinanceName(invitation.getFinanceName());
					// 头像缩略图
					invitationVo.setLitimg(invitation.getLitimg());
					
					list.add(invitationVo);
				}

			}
			rtnMap.put("total", count);
			rtnMap.put("list", list);
		} else {
			rtnMap.put("total", "0");
			rtnMap.put("list", new ArrayList<InvitationVo>());
		}
		
		return rtnMap;
	}
	
	/**
	 * 转换日期格式
	 * @param date
	 */
	private String changeDateFormat(Date date) {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");//设置日期格式
		
		return date==null ? "" : sdf.format(date);
	}

	/**
	 * 获取用户的头像信息
	 * @param userId
	 * @return rtnMap
	 * @throws Exception
	 */
	@Override
	public Map<String, Object> getInvitationById(String userId)
			throws Exception {
		Map<String,Object> rtnMap = new HashMap<>();
		// 取得我的邀请码
		String invitationCd = this.invitationDao.getMyInvitationCdById(userId);
		String count ="0";
		// 用户有邀请码
		if (invitationCd != null && !"".equals(invitationCd)) {
			// 获取我的邀请人数
			count = this.invitationDao.getInvitationCountByCd(invitationCd);
		}

		rtnMap = this.invitationDao.getInvitationById(userId);
		// login接收的数据
		rtnMap.put("count",count);
		return rtnMap;
	}
}
